const app = '/lesson-admin';
onload = () => {
    //分页查询用户数据
    pageVideo(1, 2);
};

//分页查询用户数据
function pageVideo(page, size) {
    ajax({
        'url': app + `/api/v1/user/page?page=${page}&size=${size}`,
        'success': res => {
            if (res.code < 1) {
                alert(res.message);
            }
            //查询成功
            else {
                console.log(res)
                //抓取响应数据
                let users = res.data['video-vos'];
                //抓取表格的Tbody
                let useTBody = document.querySelector('#videoTbody');
                //将查询出来的数据渲染到表格之中
                renderUsers(video, videoTBody);
                // 获取响应中的分页数据
                let page = res.data['page'];
                let size = res.data['size'];
                let pages = res.data['pages'];
                let total = res.data['total'];
                let pageNums = res.data['page-nums'];

                let pageUl = document.querySelector('#pageUl');

                // 将查询出来的分页数据渲染到分页列表中
                renderPage(page, size, pages, total, pageNums, pageUl);
            }
        }
    })
}
//添加框架
function renderVideo(users, useTBody) {
    //每次添加数据之前都清空表格的内容，防止追加
    useTBody.innerHTML = '';
    let td, tr;
    for (let i = 0, j = users.length; i < j; i++) {
        tr = document.createElement('tr');
        //第一列
        td = document.createElement('td');
        td.innerHTML = `<input type="checkbox"/>`;
        tr.appendChild(td);
        //第二列
        td = document.createElement('td');
        td.innerHTML = i + 1 + '';
        tr.appendChild(td);
        //第三列
        td = document.createElement('td');
        td.innerHTML = users[i]['username']
        tr.appendChild(td);
        //第四列
        td = document.createElement('td');
        td.innerHTML = users[i]['real-name']
        tr.appendChild(td);
        //第五列
        td = document.createElement('td');
        td.innerHTML = users[i]['nick-name']
        tr.appendChild(td);
        //第六列
        td = document.createElement('td');
        td.innerHTML = users[i]['gender']
        tr.appendChild(td);
        //第七列
        td = document.createElement('td');
        td.innerHTML = users[i]['age']
        tr.appendChild(td);
        //第八列
        td = document.createElement('td');
        td.innerHTML = users[i]['id-card']
        tr.appendChild(td);
        //第九列
        td = document.createElement('td');
        td.innerHTML = users[i]['age']
        tr.appendChild(td);
        //第十列
        td = document.createElement('td');
        td.style['text-align'] = 'center'
        td.style['padding'] = '0'
        td.innerHTML = `<a href="" class="btn btn-link btn-sm text-info px-0">详情</a>
<a href="" class="btn btn-link btn-sm text-warning px-0">修改</a>`
        tr.appendChild(td);
        //追加tr
        useTBody.appendChild(tr);
    }
}

function renderPage(page, size, pages, total, pageNums, pageUl) {
    //每次添加数据之前都清空表格的内容，防止追加
    pageUl.innerHTML = '';
    let li;
    li = document.createElement('li');
    li.classList.add('page-item');
    li.innerHTML = `<a class="page-link" href="javascript:void(0)" onclick="pageUser(${page}-1,${size})">&laquo;</a>`
    //若当前页面为1则禁用上一页
    if (page<=1){
        li.classList.add('disabled');
    }


    pageUl.appendChild(li);

    for (let i=0;i<pageNums.length;i++){
        //数字页码
        li = document.createElement('li');
        li.innerHTML=`<a class="page-link" href="javascript:void(0)" onclick="pageUser(${i+1},${size})">${pageNums}</a>`;
        pageUl.appendChild(li);
        if (i+1===page){
            li.classList.add('active');
        }
    }
    //下一页
    li=document.createElement('li');
    li.classList.add('page-item');
    li.innerHTML = `<a class="page-link" href="javascript:void(0)" onclick="pageUser(${page}+1,${size})">&raquo;</a>`
    if (page<pages){
        li.classList.add('disabled');
    }
    pageUl.appendChild(li);

}
/*修改视频信息*/
function update(video){
    // 将当前点击的用户的全部数据存储起来
    sessionStorage.setItem('video', JSON.stringify(video));
    // 跳转到用户修改页面
    location.href = '../../view/video/video.html';
}

